<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>查询单词 - 单词记忆</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: "Microsoft YaHei", sans-serif;
            background: linear-gradient(135deg, #e8f4f8 0%, #f0f8fb 100%);
            background-image:
                radial-gradient(circle at 20% 30%, rgba(64, 158, 255, 0.08) 0%, transparent 25%),
                radial-gradient(circle at 80% 70%, rgba(64, 158, 255, 0.08) 0%, transparent 25%);
            background-attachment: fixed;
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }
        .container {
            background-color: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border-radius: 16px;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.08);
            padding: 60px 40px;
            max-width: 600px;
            width: 100%;
            text-align: center;
            border: 1px solid rgba(255, 255, 255, 0.8);
        }
        .icon {
            font-size: 80px;
            margin-bottom: 30px;
            display: block;
        }
        h1 {
            color: #2c3e50;
            font-size: 32px;
            margin-bottom: 20px;
        }
        .description {
            color: #606266;
            font-size: 16px;
            margin-bottom: 40px;
        }
        .back-btn {
            padding: 12px 30px;
            background: linear-gradient(90deg, #409eff, #67c2f9);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 4px 12px rgba(64, 158, 255, 0.2);
        }
        .back-btn:hover {
            background: linear-gradient(90deg, #3088e0, #409eff);
            box-shadow: 0 6px 16px rgba(64, 158, 255, 0.3);
            transform: translateY(-2px);
        }
        .translate-section {
            width: 100%;
            margin: 30px 0;
        }
        .input-group {
            margin-bottom: 20px;
        }
        .input-group label {
            display: block;
            margin-bottom: 10px;
            color: #2c3e50;
            font-weight: 600;
            font-size: 16px;
        }
        .input-group textarea {
            width: 100%;
            min-height: 120px;
            padding: 15px;
            border: 2px solid #e0e6ed;
            border-radius: 8px;
            font-size: 15px;
            font-family: "Microsoft YaHei", sans-serif;
            resize: vertical;
            transition: all 0.3s ease;
        }
        .input-group textarea:focus {
            outline: none;
            border-color: #409eff;
            box-shadow: 0 0 0 3px rgba(64, 158, 255, 0.1);
        }
        .translate-btn {
            width: 100%;
            padding: 14px;
            background: linear-gradient(90deg, #67c23a, #85ce61);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-bottom: 20px;
            box-shadow: 0 4px 12px rgba(103, 194, 58, 0.3);
        }
        .translate-btn:hover:not(:disabled) {
            background: linear-gradient(90deg, #5daf34, #67c23a);
            transform: translateY(-2px);
            box-shadow: 0 6px 16px rgba(103, 194, 58, 0.4);
        }
        .translate-btn:disabled {
            background: #ccc;
            cursor: not-allowed;
            box-shadow: none;
        }
        .result-area {
            background: linear-gradient(135deg, #f8f9fa 0%, #ffffff 100%);
            border: 2px solid #e0e6ed;
            border-radius: 8px;
            padding: 20px;
            min-height: 120px;
            color: #2c3e50;
            font-size: 15px;
            line-height: 1.8;
            white-space: pre-wrap;
            word-wrap: break-word;
        }
        .result-area.loading {
            display: flex;
            align-items: center;
            justify-content: center;
            color: #909399;
        }
        .error-msg {
            color: #f56c6c;
            background: #fef0f0;
            border: 1px solid #fde2e2;
            padding: 12px;
            border-radius: 6px;
            margin-bottom: 15px;
            display: none;
        }
        .error-msg.show {
            display: block;
        }
        .loading-spinner {
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 3px solid rgba(64, 158, 255, 0.2);
            border-top-color: #409eff;
            border-radius: 50%;
            animation: spin 0.8s linear infinite;
            margin-right: 10px;
        }
        @keyframes spin {
            to { transform: rotate(360deg); }
        }
    </style>
    <script>
        async function checkLogin() {
            try {
                const response = await fetch('/api/user', { credentials: 'include' });
                if (!response.ok) {
                    window.location.href = '/login.html';
                    return false;
                }
                return true;
            } catch (err) {
                window.location.href = '/login.html';
                return false;
            }
        }
        window.onload = checkLogin;
        
        // 显示错误信息
        function showError(message) {
            const errorMsg = document.getElementById('errorMsg');
            errorMsg.textContent = message;
            errorMsg.classList.add('show');
            setTimeout(() => {
                errorMsg.classList.remove('show');
            }, 3000);
        }
        
        // 处理翻译请求
        async function handleTranslate() {
            const inputText = document.getElementById('inputText').value.trim();
            const resultArea = document.getElementById('resultArea');
            const translateBtn = document.getElementById('translateBtn');
            
            // 验证输入
            if (!inputText) {
                showError('请输入内容');
                return;
            }
            
            // 设置加载状态
            translateBtn.disabled = true;
            translateBtn.innerHTML = '<span class="loading-spinner"></span>翻译中...';
            resultArea.className = 'result-area loading';
            resultArea.innerHTML = '<span class="loading-spinner"></span>正在翻译，请稍候...';
            
            try {
                const response = await fetch('/api/translate', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    credentials: 'include',
                    body: JSON.stringify({ text: inputText })
                });
                
                const result = await response.json();
                
                if (result.success) {
                    resultArea.className = 'result-area';
                    resultArea.textContent = result.translation;
                } else {
                    showError(result.message || '翻译失败，请重试');
                    resultArea.className = 'result-area';
                    resultArea.textContent = '翻译失败，请重试...';
                }
            } catch (err) {
                console.error('翻译请求失败：', err);
                showError('网络错误，请检查网络连接');
                resultArea.className = 'result-area';
                resultArea.textContent = '翻译失败，请重试...';
            } finally {
                translateBtn.disabled = false;
                translateBtn.textContent = '开始翻译';
            }
        }
        
        // 支持回车键触发翻译
        document.addEventListener('DOMContentLoaded', () => {
            const inputText = document.getElementById('inputText');
            if (inputText) {
                inputText.addEventListener('keydown', (e) => {
                    if (e.ctrlKey && e.key === 'Enter') {
                        handleTranslate();
                    }
                });
            }
        });
    </script>
</head>
<body>
<div class="container">
    <span class="icon">🔍</span>
    <h1>智能翻译</h1>
    <p class="description">支持中英互译，输入即可自动识别语言</p>
    
    <div class="error-msg" id="errorMsg"></div>
    
    <div class="translate-section">
        <div class="input-group">
            <label for="inputText">输入文本（中文/英文）</label>
            <textarea id="inputText" placeholder="请输入要翻译的内容..."></textarea>
        </div>
        
        <button class="translate-btn" id="translateBtn" onclick="handleTranslate()">开始翻译</button>
        
        <div class="input-group">
            <label>翻译结果</label>
            <div class="result-area" id="resultArea">翻译结果将显示在这里...</div>
        </div>
    </div>
    
    <button class="back-btn" onclick="window.location.href='/home.html'">返回首页</button>
</div>
</body>
</html>
